package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理类")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页查询")
    @ApiOperationSupport(includeParameters = {"userDto.dataState","userDto.deptNo","userDto.email",
            "userDto.postNo","userDto.realName","userDto.roleId"})
    public ResponseResult<PageResponse<UserVo>> selectByPage(@RequestBody UserDto userDto,
                                                             @PathVariable Integer pageNum,
                                                             @PathVariable Integer pageSize){
        PageResponse<UserVo> pageResponse = userService.selectByPage(userDto,pageNum,pageSize);
        return success(pageResponse);
    }


    @PutMapping
    @ApiOperation(value = "用户添加")
    @ApiOperationSupport(includeParameters = {"userDto.realName","userDto.roleVoIds","userDto.deptNo"
            ,"userDto.postNo","userDto.dataState","userDto.email","userDto.mobile"})
    public ResponseResult<Boolean> addUser(@RequestBody UserDto userDto){
        Boolean isSuccess = userService.addUser(userDto);
        return success(isSuccess);
    }


    @PostMapping("/list")
    @ApiOperation(value = "查询用户列表")
    @ApiOperationSupport(includeParameters = {"userDto.realName"})
    public ResponseResult<List<UserVo>> selectUserList(@RequestBody UserDto userDto){
        List<UserVo> list = userService.selectUserList(userDto);
        return success(list);

    }

    @PatchMapping
    @ApiOperation(value = "用户修改")
    @ApiOperationSupport(includeParameters = {})
    public ResponseResult<Boolean> updateUser(@RequestBody UserDto userDto){
        Boolean isSuccess = userService.updateUser(userDto);
        return success(isSuccess);
    }

    @GetMapping("/current-user")
    @ApiOperation(value = "获取当前用户")
    public ResponseResult<UserVo> getCurrent(){
        return success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "启用/禁用")
    public ResponseResult isEnable(@PathVariable Long id, @PathVariable String status){
        userService.updateStatus(id,status);
        return success();
    }

    @DeleteMapping("/remove/{userId}")
    @ApiOperation(value = "删除用户")
    public ResponseResult deleteUser(@PathVariable String userId){
        userService.deleteUser(userId);
        return success();
    }


    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "密码重置")
    public ResponseResult<Boolean> resetPassword(@PathVariable String userId){
        Boolean isSuccess = userService.resetPassword(userId);
        return success(isSuccess);
    }

}
